package com.ht.managermentserver.security;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.ht.managermentserver.entity.SysUser;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.Collections;

/**
 * @author czy
 * @package com.ht.managermentserver.security
 * @date 2020/6/1 9:21
 */
public class JwtUser implements UserDetails {
  private static final long serialVersionUID = -5292718458574832793L;
  @JsonIgnore private final Long id;
  private final String username;
  @JsonIgnore private final String password;
  private final Collection<? extends GrantedAuthority> authorities;
  public JwtUser(final SysUser user) {
    this.id = user.getId();
    this.username = user.getUserName();
    this.password = user.getPassWord();
    this.authorities = Collections.singleton(new SimpleGrantedAuthority("user"));
  }

  public Long getId() {
    return id;
  }

  @Override
  public Collection<? extends GrantedAuthority> getAuthorities() {
    return authorities;
  }

  @Override
  public String getPassword() {
    return password;
  }

  @Override
  public String getUsername() {
    return username;
  }

  @Override
  public boolean isAccountNonExpired() {
    return true;
  }

  @Override
  public boolean isAccountNonLocked() {
    return true;
  }

  @Override
  public boolean isCredentialsNonExpired() {
    return true;
  }

  @Override
  public boolean isEnabled() {
    return true;
  }

  @Override
  public String toString() {
    return "JwtUser{"
        + "id="
        + id
        + ", username='"
        + username
        + '\''
        + ", password='"
        + password
        + '\''
        + ", authorities="
        + authorities
        + '}';
  }
}
